package com.apache.portal.common.websocket;

import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * description: WebsocketClient
 *
 * @author dyh 创建时间：2018年01月26日
 */
public class WebsocketEndpoint extends WebSocketClient {

    /**
     * 获取实例WebSocketClient对象
     *
     * @param configKey
     * @param url
     * @param requestParams
     * @return
     */
    public static WebsocketEndpoint getWebsocketClient(String configKey, String url,
            String requestParams) throws URISyntaxException {
        String urlPath = StrUtil.doNull(ToolsUtil.getInstance().getValueByKey(configKey), url);
        WebsocketEndpoint websocketClient = new WebsocketEndpoint(
                new URI(urlPath + StrUtil.doNull(requestParams, "")), new Draft_6455());
        websocketClient.connect();
        return websocketClient;
    }

    public WebsocketEndpoint(URI serverUri, Draft draft) {
        super(serverUri, draft);
    }

    public WebsocketEndpoint(URI serverURI) {
        super(serverURI);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {

    }

    @Override
    public void onMessage(String s) {

    }

    @Override
    public void onClose(int i, String s, boolean b) {

    }

    @Override
    public void onError(Exception e) {

    }

}
